package com.far;

import android.app.Activity;
import android.content.SharedPreferences;
import android.graphics.PixelFormat;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.TextView;

public class SettingsPage extends Activity implements OnClickListener{
	private TextView tv_sound, tv_music, tv_vibrate, tv_back;
    private boolean[] settingsHolder = new boolean[3];
    
	@Override
	public void onCreate(Bundle savedInstanceState) {
	    super.onCreate(savedInstanceState);
	    setContentView(R.layout.settings);  
	    
	    tv_sound = (TextView) findViewById(R.id.sound);
	    tv_music = (TextView) findViewById(R.id.music);
	    tv_vibrate = (TextView) findViewById(R.id.vibrate);
	    tv_back = (TextView) findViewById(R.id.back);
	    
	    tv_sound.setOnClickListener(this);
	    tv_music.setOnClickListener(this);
	    tv_vibrate.setOnClickListener(this);
	    tv_back.setOnClickListener(this);
	    
	    getSettings();
	    setText();        
	}
	
	@Override
	public void onAttachedToWindow(){
		super.onAttachedToWindow();
		Window window = getWindow();
		window.setFormat(PixelFormat.RGBA_8888);
	}
    

    private void setText() {
            tv_sound.setText(settingsHolder[0] ? "Sound: OFF" : "Sound: ON" );
            tv_music.setText(settingsHolder[1] ? "Music: OFF" : "Music: ON" );
            tv_vibrate.setText(settingsHolder[2] ? "Vibration: OFF" : "Vibration: ON" );
    }

    private void getSettings() {
            SharedPreferences settings = getSharedPreferences(MainMenu.PREF_NAME, 0);
            settingsHolder[0] = settings.getBoolean("sound", true);
            settingsHolder[1] = settings.getBoolean("music", true);
            settingsHolder[2] = settings.getBoolean("vibrate", true);                               
    }

    @Override
    public void onClick(View v) {
            SharedPreferences settings = getSharedPreferences(MainMenu.PREF_NAME, 0);
            SharedPreferences.Editor editor = settings.edit();
        
        switch(v.getId()){
            case R.id.sound:
                    editor.putBoolean("sound", !settingsHolder[0]);
                    editor.commit();
                    break;
            case R.id.music:
                    editor.putBoolean("music", !settingsHolder[1]);
                    editor.commit();
                    break;
            case R.id.vibrate:
                    editor.putBoolean("vibrate", !settingsHolder[2]);
                    editor.commit();
                    break;
            case R.id.back:
                    finish();
                    break;
    }
            getSettings();
            setText();
            
        
    }
    
}
